package com.hm.oper_monitor;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.springframework.stereotype.Component;

import com.hm.common.util.ResourceUtil;
import com.hm.common.util.SpringUtil;
import com.hm.oper_monitor.bean.ResourcesManager;
import com.hm.oper_monitor.constants.Constants;
import com.hm.oper_monitor.init.Init;

@Component
@SuppressWarnings("serial")
public class Loading extends JFrame {
	public static JPanel bgPanel;
	ResourcesManager rm;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		new Loading();
	}

	private void init() {
		rm = SpringUtil.getBean(ResourcesManager.class);
		rm.initScreenSize();
		initBackgroundPanel();
		this.setBounds(rm.getScreenViewProp().getScreenX() * 4, rm.getScreenViewProp().getScreenY() * 3,
				rm.getScreenViewProp().getScreenWidth() / 3, rm.getScreenViewProp().getScreenHeight() / 3);
		this.setUndecorated(true);
		this.setContentPane(bgPanel);
		this.setVisible(true);
		
	}

	/**
	 * 给Frame增加背景面板
	 * 
	 * @return
	 */
	public JPanel initBackgroundPanel() {
		Image loadingImage = ResourceUtil.getImage(this.getClass().getClassLoader(), "images/timg.jpg");
		ImageIcon loadingIcon = new ImageIcon();
		loadingIcon.setImage(loadingImage);
		loadingIcon.setImage(loadingIcon.getImage().getScaledInstance(500, 300, Image.SCALE_DEFAULT));

		JLabel imageLabel = new JLabel(loadingIcon);
		bgPanel = new JPanel();
		bgPanel.setLayout(new BorderLayout());
		bgPanel.setBounds(0, 0, rm.getScreenViewProp().getScreenWidth(), rm.getScreenViewProp().getScreenHeight());
		bgPanel.add(BorderLayout.CENTER, imageLabel);
		
		final String[] configNames = { "Loading", "Configuring database", "Initializing initial settings", "Configuration initialization completed", "Initializing interface settings", "The initialization interface is complete", "Initialize the database", "Initialize the database to complete", "Load local configuration", "Load local configuration complete", "Loading completed" };
		final JProgressBar jpb = new JProgressBar();
		jpb.setMinimum(1);
		jpb.setMaximum(configNames.length);
		jpb.setStringPainted(true);
		final JFrame t = this;
		
		// 创建线程显示进度  
        new Thread(){   
            public void run(){  
                for (int i = 0;  i < configNames.length; i++) {  
                    try {  
                        Thread.sleep(400);  //   让当前线程休眠0.4ms  
                        // 设置提示信息  
                        jpb.setString(configNames[i]);
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    jpb.setValue(i); // 设置进度条数值  
                }  
                jpb.setValue(configNames.length);
                jpb.setString("Loading completed");
                EventQueue.invokeLater(new Runnable() {
        			public void run() {
        				try {
        					SpringUtil.getBean(OperMonitor.class);
        				} catch (Exception e) {
        					e.printStackTrace();
        				}
					}
				});
                t.setVisible(false);
			}
		 }.start(); //  启动进度条线程 
		this.setVisible(false);
		bgPanel.add(BorderLayout.SOUTH,jpb);
		return bgPanel;
	}

	/**
	 * Create the frame.
	 */
	public Loading() {
		this.init();
		// Init init = new Init(bgPanel);
		// Thread t = new Thread(init);
		// t.start();
	}

	
}
